const api = require('../../../../api')
import { request } from '../../../../util'
import Notify from '@vant/weapp/notify/notify'

Component({
  //externalClasses: ['custom-class'],
  properties: {
    category: {
      type: Array
    },
    vagelist: {
      type: Array
    }
  },
  data: {
    Idindex: 0,
    isok: true,
    show: false,
    vagelistdata: [],
    checkvagedata: {
      totalNum: 0,
      vegetables: ''
    },
    checkindex: 0,
    checkindex2: 0,
    isadd: true,
    isupdate: true,
    batchformData: [],
    userInfo: wx.getStorageSync('userInfo'),
    checkindexarr:[]
  },

  methods: {
    onParentChange(event) {
      this.setData({
        Idindex: event.detail.index
      })
      // this.setActiveKey(event.detail.index, 0).then(() => {    wx-for="{{category[index].children.length}}"
      //   this.triggerEvent('change', [this.data.activeKey, this.data.subActiveKey]);
      // });
    },
    createBatch() {
      //生成批次
    },
    addBatch() {
      //继续添加商品
      var data = this.data.category[this.data.Idindex]
      var obj = []
      for (var i = 0; i < this.data.vagelist.length; i++) {
        var index = 0
        var dt = this.data.vagelist[i]
        for (var j = 0; j < data.batchDetailList.length; j++) {
          var dt2 = data.batchDetailList[j]
          if (dt.vegetablesId != dt2.vageId) {
            index += 1
          }
        }
        if (index == data.batchDetailList.length) {
          obj.push(dt)
        }
      }
      this.setData({
        show: true,
        vagelistdata: obj,
        isupdate:false
      })
    },
    defaultdata() {
      
    },
    checkvage(e) {
      var index = e.currentTarget.dataset.index
      if (!this.data.vagelistdata[index].checked) {
        this.data.vagelistdata[index].totalNum = this.data.checkvagedata.totalNum
        this.setData({
          checkvagedata: this.data.vagelistdata[index],
          isadd: false,
          checkindex: index,
          isupdate: false
        })
      }
    },
    reback() {
      this.setData({
        isadd: true,
        checkvagedata: {
          totalNum: 0
        }
      })
    },
    sureAdd() {
      //添加批次数据
      console.log(123)
      var batchcode = this.data.category[this.data.Idindex].batchCode
      var batchId = this.data.category[this.data.Idindex].batchId
      var that = this
      if (this.data.batchformData.length == 0) {
        Notify({ type: 'danger', message: '请输入预购单' })
        return false
      }
      var obj = {
        data: JSON.stringify(this.data.batchformData),
        userId: this.data.userInfo.id,
        roleId: this.data.userInfo.roleId,
        batchId: batchId,
        batchCode: batchcode
      }
      request(api.updateInsertBatch, {
        data: JSON.stringify(this.data.batchformData),
        userId: this.data.userInfo.id,
        roleId: this.data.userInfo.roleId,
        batchId: batchId,
        batchCode: batchcode
      }, 'post').then(function (res) {
        console.log(res)
        if (res.code == 200) {
          Notify({ type: 'success', message: '操作成功' })
          that.data.checkindexarr.filter(elem => {
            if (that.data.vagelist[elem].checked) {
              let update = `vagelist[${elem}].checked`
              that.setData({
                [update]: false
              })
            }
          })
          that.setData({
            show: false,
            checkvagedata: {},
            batchformData: [],
            checkindex: 0,
            index: 0
          })
          that.batchdata(batchcode)
        } else {
          Notify({ type: 'danger', message: res.msg })
        }
      })
    },
    onclosepopup() {
      this.setData({
        show: false
      })
    },
    editbatchdata(e){
      //编辑批次信息
      this.setData({
        isadd: false,
        checkvagedata:this.data.batchformData[e.currentTarget.dataset.id],
        isupdate: true,
        checkindex2: e.currentTarget.dataset.id
      })
    },
    editbatchdata2(e){
      //编辑批次信息
      this.setData({
        isadd: false,
        checkvagedata:this.data.category[this.data.Idindex].batchDetailList[e.currentTarget.dataset.id],
        isupdate: true,
        checkindex2: e.currentTarget.dataset.id
      })
    },
    //添加或修改数据(未进数据库)
    addbatchform(e) {
      console.log(this.data.isupdate)
      if (!this.data.isupdate) {
        //添加
        var obj = {
          totalNum: this.data.checkvagedata.totalNum,
          vegetables: this.data.checkvagedata.vegetables,
          vageId: this.data.checkvagedata.vegetablesId,
          checkindex:this.data.checkvagedata.checkindex
        }
        let update = `vagelistdata[${this.data.checkindex}].checked`
        console.log(this.data.checkindex)
        console.log(this.data.vagelistdata[this.data.checkindex])
        var obj2 =this.data.batchformData.concat(obj)
        var indexarr =this.data.checkindexarr.concat(this.data.checkindex)
        this.setData({
          isadd: true,
          checkvagedata: {
            totalNum: 0
          },
          batchformData: obj2,
          [update]: true,
          checkindexarr: indexarr
        })
      } else{
         console.log(this.data.checkvagedata)
         console.log(this.data.batchformData[this.data.checkindex2])
         var up = `batchformData[${this.data.checkindex2}]`
         this.setData({
          'batchformData':this.data.batchformData,
          isadd:true
         })
      }
    },
    //添加或修改数据(进数据库)
    addbatchform2(e){
      if (!this.data.isupdate) {
        //添加
        var obj = {
          totalNum: this.data.checkvagedata.totalNum,
          vegetables: this.data.checkvagedata.vegetables,
          vageId: this.data.checkvagedata.vegetablesId,
          checkindex:this.data.checkvagedata.checkindex
        }
        let update = `vagelistdata[${this.data.checkindex}].checked`
        console.log(this.data.checkindex)
        console.log(this.data.vagelistdata[this.data.checkindex])
        var obj2 =this.data.batchformData.concat(obj)
        var indexarr =this.data.checkindexarr.concat(this.data.checkindex)
        this.setData({
          isadd: true,
          checkvagedata: {
            totalNum: 0
          },
          batchformData: obj2,
          [update]: true,
          checkindexarr: indexarr
        })
      } else{
        var that = this
        var obj = this.data.category[this.data.Idindex].batchDetailList[this.data.checkindex2];
        console.log(obj)
        console.log(this.data.checkvagedata)
        request(api.updateDetail,{str:JSON.stringify(obj)},"post").then(function(res){
           console.log(res);
           if(res.code==200){
            wx.showToast({
              title: '操作成功',
              icon: 'success',
              duration: 2000//持续的时间
            })
            that.setData({
              isadd:true,
              category:that.data.category
            })
            console.log(that.data.category)
           }else{
            Notify({ type: 'danger', message: res.msg })
           }
         })
      }
    },
    changenum(e){
      this.setData({
        'checkvagedata.totalNum':e.detail
      })
    },
    //删除（进数据库）
    delbatchdata(e){
       console.log(e)
       var that = this;
       request(api.delDetail,{id:e.currentTarget.dataset.keyid},'POST').then(function(res){
         if(res.code==200){
          wx.showToast({
            title: '操作成功',
            icon: 'success',
            duration: 2000//持续的时间
          })
          console.log(that.data.category[that.data.Idindex].batchDetailList)
          // var obj = that.data.category[that.data.Idindex].batchDetailList.filter(elem=>{
          //    return elem.batchDetailId != e.currentTarget.dataset.keyid
          // })
          // var up = `that.data.category[${that.data.Idindex}].batchDetailList`
          // that.setData({
          //   up:obj
          // })
          that.batchdata(that.data.category[that.data.Idindex].batchCode)
         }else{
          // wx.showToast({
          //   title: res.msg,
          //   icon: 'success',
          //   duration: 2000//持续的时间
          // })
         }
       })
    },
    //删除（未进数据库）
    delbatchdata2(e){
      console.log(e)
      var index = e.currentTarget.dataset.id
      var checkindex = e.currentTarget.dataset.keyid
      var obj = this.data.batchformData.filter(elem => {
      return elem.vageId != this.data.batchformData[index].vageId
    })
      var update = `vagelistdata[${checkindex}].checked`
      this.setData({
        batchformData: obj,
        [update]: false,
      })
    },
    batchdata(code){
      var that = this;
      //批次数据
      request(api.batchdata,{batchcode:code},'post').then(function(res){
          console.log(res)
          that.setData({
            category:res.data.data
          })
      })
    },
    createBatch(){
      //查询库存
      console.log(this.data.category[this.data.Idindex])
       

    }

    // onChildChange(event) {
    //   this.setActiveKey(this.data.activeKey, event.detail.index).then(() => {
    //     this.triggerEvent('change', [this.data.activeKey, this.data.subActiveKey]);
    //   });
    // },
    // changCategory(event) {
    //   const { item } = event.currentTarget.dataset;
    //   this.triggerEvent('changeCategory', {
    //     item,
    //   });
    // },
  },
  onShow() {
    this.defaultdata()
  }
})
